package com.qf.commerce.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.commerce.common.dto.RoleDto;
import com.qf.commerce.common.vo.AccountVo;
import com.qf.commerce.common.vo.RoleVo;
import com.qf.commerce.entity.Role;
import com.qf.commerce.mapper.RoleMapper;
import com.qf.commerce.service.RoleService;
import com.qf.commerce.utils.CommonBeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Resource
    private RoleMapper roleMapper;

    @Override
    public int add(RoleDto roleDto) {
        int i = 0;
        i = roleMapper.insertRole(roleDto);
        if (i <= 0) {
            throw new RuntimeException("添加失败");
        }
        return i;
    }

    @Override
    public int updateRole(Role role,Long roleId) {
        int i = 0;
        i = roleMapper.updateRole(role, roleId);
        if (i <= 0) {
            throw new RuntimeException("更新失败");
        }
        return i;
    }

    @Override
    public int updatePower(Long parentPowerId, Integer level, Long roleId) {
        int i = 0;
        i = roleMapper.updatePower(parentPowerId, level, roleId);
        if (i <= 0) {
            throw new RuntimeException("更新权限失败");
        }
        return i;
    }

    @Override
    public int deleteRole(List<Long> ids) {
        int i = 0;
        i = roleMapper.deleteRole(ids);
        if (i <= 0) {
            throw new RuntimeException("删除失败");
        }
        return i;
    }


    @Override
    public List<RoleDto> selectRoleDetail(Long roleId) {
        return roleMapper.selectRoleDetail(roleId);
    }

    @Override
    public PageInfo<RoleVo> selectRole(String organizationName, Integer status, String roleName, String userName, Integer page, Integer size) {
        return CommonBeanUtils.copyPage(PageHelper.startPage(page,size).doSelectPageInfo(() -> roleMapper.selectRole(organizationName,status,roleName,userName)),new PageInfo<RoleVo>(),RoleVo::new);
    }



}
